#include "pch.h"

#include "core/rtti.h"
#include "core/referencecount.h"



namespace Core
{

	///
	Rtti::Rtti(const char *name, const Helper::FCC &fcc, Creator creatorFnc, Core::Rtti *parent, tSize instSize):
m_typeFCC(fcc),
	m_parent(parent),
	creator(creatorFnc),
	m_instSize(instSize)
{
	this->m_typeName = name;

	if(!Factory::Instance()->ClassExists(this->m_typeName))
		Factory::Instance()->Register(this, this->m_typeName, fcc);
}





///
bool Rtti::operator ==(const Core::Rtti &rhs) const
{
	return this == &rhs;
}


///
bool Rtti::operator !=(const Core::Rtti & rhs) const 
{
	return this != &rhs;
}


///
const Helper::String & Rtti::GetName() const 
{
	return this->m_typeName;
}


///
const Helper::FCC Rtti::GetFCC() const 
{
	return this->m_typeFCC;
}


///
const Core::Rtti * Rtti::GetParent() const 
{
	return this->m_parent;
}


///
tSize Rtti::GetInstanceSize() const 
{
	return this->m_instSize;
}


///
ReferenceCount * Rtti::Create() const 
{
	if(this->creator == 0)
		return NULL;
	else
		return this->creator();
}


///
bool Rtti::DerivesFrom(const Core::Rtti &other) const
{
	const Rtti * cur;

	for(cur = this; cur != 0; cur = cur->GetParent())
	{
		if(cur == &other)
			return true;
	}

	return false;
}


///
bool Rtti::DerivesFrom(const Helper::String & name) const 
{
	const Rtti * cur;

	for(cur = this; cur != 0; cur = cur->GetParent())
	{
		if(cur->m_typeName == name)
			return true;
	}

	return false;
}


///
bool Rtti::DerivesFrom(const Helper::FCC & fcc) const 
{
	const Rtti * cur;

	for(cur = this; cur != 0; cur->GetParent())
	{
		if(cur->m_typeFCC == fcc)
			return true;
	}

	return false;
}



///
void * Rtti::AllocInstanceMemory()
{
	return Memory::MemoryManager::Alloc(Memory::DataHeap, this->m_instSize);
}


///
void Rtti::FreeInstanceMemory(void * ptr)
{
	Memory::MemoryManager::Free(Memory::DataHeap, ptr);
}

}